package com.douziit.meilianoa.base.response;




import com.douziit.meilianoa.base.enums.ResponseCode;

import java.util.HashMap;
import java.util.Map;

/**
 * 返回Json数据格式响应
 *
 * @author MrShun
 * @version 1.0
 * @Date 2017/11/10
 */
public class ResponseUtil {


    /**
     * 操作成功
     *
     * @param data      数据对象
     * @param startTime 开始时间
     * @param <T>
     * @return
     */
    public static <T> ResponseData jsonSuccess(T data, Long startTime) {
        if (data == null) {
            return jsonFail(ResponseCode.DATA_EXIT_INFO);
        }
        ResponseData resp = new ResponseData();
        resp.setCode(ResponseCode.SUCCESS.getCode());
        resp.setMessage(ResponseCode.SUCCESS.getMsg());
        if (startTime != null) {
            resp.setTime(System.currentTimeMillis() - startTime + " msec");
        }

        if (data instanceof Page) {
            Page page = (Page) data;
            resp.setData(page.getData());
            resp.setIndex(page.getIndex());
            resp.setSize(page.getSize());
            resp.setTotal(page.getTotal());
            resp.setPages(page.getPages());
        } else {
            resp.setData(data);
        }
        return resp;
    }

    public static <T> ResponseData jsonSuccess(T data) {
        return jsonSuccess(data, null);
    }

    /**
     * 操作异常失败
     *
     * @param responseCode 枚举响应码
     * @param exception    异常信息
     * @return
     */
    public static ResponseData jsonException(ResponseCode responseCode, String exception) {
        ResponseData resp = new ResponseData();
        resp.setCode(responseCode.getCode());
        resp.setMessage(responseCode.getMsg());
        if (exception != null) {
            resp.setException(exception);
        }
        return resp;
    }

    public static ResponseData jsonException(ResponseCode responseCode) {
        return jsonException(responseCode, null);
    }

    /**
     * 操作失败
     *
     * @param responseCode 枚举响应码
     * @param code         自定义code
     * @param msg          自定义消息提示
     * @return
     */
    public static ResponseData jsonFail(ResponseCode responseCode, Integer code, String msg) {
        ResponseData resp = new ResponseData();
        if (responseCode == null) {
            resp.setCode(code == null ? ResponseCode.TIPS_ERROR.getCode() : code);
            resp.setMessage(msg);
        } else {
            resp.setCode(responseCode.getCode());
            resp.setMessage(responseCode.getMsg());
            if (msg != null) {
                resp.setException(msg);
            }
        }
        return resp;
    }

    /**
     * 操作错误
     *
     * @param code  自定义code
     * @param msg   自定义消息提示
     * @param error 第三方消息提示
     * @return
     */
    public static ResponseData jsonError(Integer code, String msg, String error) {
        ResponseData resp = new ResponseData();
        resp.setCode(code);
        resp.setMessage(msg);
        resp.setException(error);
        return resp;
    }

    public static <T> Map uiResponse(T data) {
        Map map = new HashMap(10);
        map.put("code", ResponseCode.SUCCESS.getCode());
        map.put("message", ResponseCode.SUCCESS.getMsg());
        if (data.getClass() == Page.class) {
            Page page = (Page) data;
            map.put("data", page.getData());
            map.put("total", page.getTotal());
        } else {
            map.put("data", data);
        }
        return map;
    }

    public static ResponseData jsonFail(String msg) {
        return jsonFail(null, null, msg);
    }

    public static ResponseData jsonFail(Integer code, String msg) {
        return jsonFail(null, code, msg);
    }

    public static ResponseData jsonFail(ResponseCode responseCode, String msg) {
        return jsonFail(responseCode, null, msg);
    }

    public static ResponseData jsonFail(ResponseCode responseCode) {
        return jsonFail(responseCode, null, null);
    }

}
